'''
    爬取LOL壁纸语音
'''
import requests
import json
import os

if not os.path.exists('audio'):
    os.mkdir('audio')
if not os.path.exists('img'):
    os.mkdir('img')

# 英雄列表json文件链接
herolist = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
# 获取
herolistjson = json.loads(requests.utils.unquote(requests.get(herolist).text))

for hero in herolistjson["hero"]:
    # 爬取语音
    if not os.path.exists(f'audio\\{hero["title"]}'):
        os.mkdir(f'audio\\{hero["title"]}')

    selectAudio = requests.get(hero['selectAudio'])
    with open(f'audio\\{hero["title"]}\\selectAudio.ogg','wb') as f:
        f.write(selectAudio.content)
        print(f'{hero["title"]}\\selectAudio.ogg is ok!')

    banAudio = requests.get(hero['banAudio'])
    with open(f'audio\\{hero["title"]}\\banAudio.ogg','wb') as f:
        f.write(banAudio.content)
        print(f'{hero["title"]}\\banAuio.ogg is ok!')

    # 爬取图片
    if not os.path.exists(f'img\\{hero["title"]}'):
        os.mkdir(f'img\\{hero["title"]}')

    imgjsonurl = 'https://game.gtimg.cn/images/lol/act/img/js/hero/' + hero['heroId'] + '.js'
    r = requests.get(imgjsonurl)
    jsonr = json.loads(r.text)
    for skin in jsonr["skins"]:
        try:
            img = requests.get(skin["mainImg"])
            with open(f'img\\{hero["title"]}\\{skin["name"]}.jpg','wb') as f:
                f.write(img.content)
            print(f'{hero["title"]}\\{skin["name"]}.jpg is ok!')
        except:pass
